home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / Papers / C++ Exceptions / µShell / Core Utilities / Debugging / DebugWrite.cp < prev    next >
Encoding:
Text File  |  1998-06-06  |  5.5 KB  |  273 lines  |  [TEXT/CWIE]

  1. #ifndef __DEBUGWRITE__
  2. #include "DebugWrite.h"
  3. #endif
  4. #ifndef __AEPRINT__
  5. #include "AEPrint.h"
  6. #endif
  7. #ifndef __EXCEPTIONS__
  8. #include "Exceptions.h"
  9. #endif
  10. #ifndef __MEMUTILS__
  11. #include "MemUtils.h"
  12. #endif
  13.  
  14. #ifndef __TEXTUTILS__
  15. #include <TextUtils.h>
  16. #endif
  17.  
  18. #include <string.h>
  19. #include <stdio.h>
  20.  
  21. extern "C" {
  22.  
  23. pascal void DumpAE(const AEDesc* desc);
  24.  
  25. }
  26.  
  27. static const Byte        gOpenQuote[]        = "\p‘";
  28. static const Byte        gCloseQuote[]        = "\p’";
  29. bool                    gNeverTrue            = false;
  30.  
  31. static DebugWriteProcPtr    gDebugWriteProc;
  32.  
  33. //------------------------------------------------------------------------------
  34.  
  35. DebugWriteProcPtr GetDebugWriteProc()
  36. {
  37.     return gDebugWriteProc;
  38. }
  39.  
  40. //------------------------------------------------------------------------------
  41.  
  42. DebugWriteProcPtr SetDebugWriteProc(DebugWriteProcPtr newProc)
  43. {
  44.     DebugWriteProcPtr oldProc = gDebugWriteProc;
  45.     
  46.     gDebugWriteProc = newProc;
  47.     
  48.     return oldProc;
  49. }
  50.  
  51. //------------------------------------------------------------------------------
  52.  
  53. void StdDebugWriteProc(const void* xdata, Size size, DebugAction action)
  54. {
  55.     static Str255            sBuffer        = {0};
  56.     static const Byte        sNewline[2] = { 1, 0x0d };
  57.  
  58.     Byte*    data  = (Byte*) xdata;
  59.     long    count = sBuffer[0];
  60.  
  61.     while (size > 0)
  62.     {
  63.         long extra = count + size - 255; // Bytes left over after after copy
  64.         long copy  = (extra > 0) ? size - extra : size;         // Bytes to copy
  65.         
  66.         if (copy > 0)     // copy what will fit
  67.         {
  68.             BlockMoveData(data, &sBuffer[count+1], copy);
  69.             sBuffer[0] = count + copy;
  70.             data += copy;
  71.             size -= copy;
  72.         }
  73.  
  74.         if (extra >= 0)   // More left...
  75.         {
  76.             SysBreakFunc(sBuffer);
  77.             sBuffer[0] = count = 0;
  78.         } 
  79.     }
  80.     
  81.     if (action & kDebugWriteNewline)
  82.     {
  83.         StdDebugWriteProc(&sNewline[1], 1, kDebugWrite);
  84.     }
  85.     
  86.     if (sBuffer[0] && (action & kDebugWriteFlush))
  87.     {
  88.         SysBreakFunc(sBuffer);
  89.         sBuffer[0] = 0;
  90.     }
  91.  
  92.     if (action & kDebugBreak)
  93.     {
  94.         Debugger();
  95.     }
  96. }
  97.  
  98. //------------------------------------------------------------------------------
  99.  
  100. void DebugWritePtr(const void* data, Size size, DebugAction action)
  101. {
  102.     if (gDebugWriteProc != nil)
  103.     {
  104.         (*gDebugWriteProc)(data, size, action);
  105.     }
  106. }
  107.  
  108. //------------------------------------------------------------------------------
  109.  
  110. void DebugWrite(const unsigned char* str, DebugAction action)
  111. {
  112.     DebugWritePtr(&str[1], str[0], action);
  113. }
  114.  
  115. //------------------------------------------------------------------------------
  116.  
  117. void DebugWrite(const char* str, DebugAction action)
  118. {
  119.     DebugWritePtr(str, strlen(str), action);
  120. }
  121.  
  122. //------------------------------------------------------------------------------
  123.  
  124. void DebugWriteErr(long num, DebugAction action)
  125. {
  126.     Str15 str;
  127.     
  128.     NumToString(num, str);
  129.     DebugWrite(str, action);
  130. }
  131.  
  132. //------------------------------------------------------------------------------
  133.  
  134. void DebugWriteNum(long num, DebugAction action)
  135. {
  136.     Str15 str;
  137.     
  138.     NumToString(num, str);
  139.     DebugWrite(str, action);
  140. }
  141.  
  142. //------------------------------------------------------------------------------
  143.  
  144. void DebugWriteHex(long num, DebugAction action)
  145. {
  146.     static const char* hex = "0123456789ABCDEF";
  147.  
  148.     char buf[8];
  149.         
  150.     for (int i = 7; i >= 0; i--, num >>= 4)
  151.     {
  152.         buf[i] = hex[num & 0x0F];
  153.     }
  154.  
  155.     DebugWritePtr(buf, 8, action);
  156. }
  157.  
  158. //------------------------------------------------------------------------------
  159.  
  160. void DebugWriteLn(const unsigned char* str, bool breakToDebugger)
  161. {
  162.     DebugWrite(str, breakToDebugger ? kDebugWriteLnBreak : kDebugWriteLn);
  163. }
  164.  
  165. //------------------------------------------------------------------------------
  166.  
  167. void DebugWriteType(OSType type, DebugAction action)
  168. {
  169.     DebugWrite((const StringPtr) gOpenQuote);
  170.     DebugWritePtr(&type, sizeof(type));
  171.     DebugWrite((const StringPtr) gCloseQuote, action);
  172. }
  173.  
  174. //------------------------------------------------------------------------------
  175.  
  176. void DebugWriteAE(const AEDesc* desc, DebugAction action)
  177. {
  178.     char buffer[1024];
  179.     long stringLength;
  180.  
  181.     char*   p = nil;
  182. //    Handle    h = nil
  183.     OSErr err = LogIfErr(AEPrintSize(desc, &stringLength));
  184.     
  185.     if (err == noErr)
  186.     {
  187.         if (stringLength > sizeof(buffer))
  188.         {
  189.             p = NewPtr(stringLength);
  190.  
  191.             if (p == nil)
  192.             {
  193.                 LogIfErr(MemError());
  194.             }
  195.         }
  196.  
  197. //        WithHandleLocked tmp(desc->dataHandle, true);
  198.  
  199.         err = LogIfErr(AEPrint(desc, p ? p : buffer, p != nil ? stringLength : sizeof(buffer)));
  200.  
  201.         if (err == noErr)
  202.         {
  203.             DebugWrite(p ? p : buffer, action);
  204.         }
  205.  
  206.         if (p != nil)
  207.         {
  208.             DisposePtr(p);
  209.         }
  210.     }
  211.     else if (gNeverTrue)    // prevent DumpAE from being dead-stripped
  212.     {
  213.         DumpAE(desc);
  214.     }
  215. }
  216.  
  217. //------------------------------------------------------------------------------
  218.  
  219. pascal void DumpAE(const AEDesc* desc)
  220. {
  221.     char buffer[1024];
  222.     long stringLength;
  223.  
  224.     char*   p = nil;
  225.     OSErr err = LogIfErr(AEPrintSize(desc, &stringLength));
  226.     
  227.     if (err == noErr)
  228.     {
  229.         if (stringLength > sizeof(buffer))
  230.         {
  231.             p = NewPtr(stringLength);
  232.  
  233.             if (p == nil)
  234.             {
  235.                 LogIfErr(MemError());
  236.             }
  237.         }
  238.  
  239. //        WithHandleLocked tmp(desc->dataHandle, true);
  240.  
  241.         err = LogIfErr(AEPrint(desc, p ? p : buffer, p != nil ? stringLength : sizeof(buffer)));
  242.  
  243.         if (err == noErr)
  244.         {
  245.             printf(p);
  246.         }
  247.  
  248.         if (p != nil)
  249.         {
  250.             DisposePtr(p);
  251.         }
  252.     }
  253. }
  254.  
  255. //------------------------------------------------------------------------------
  256.  
  257. void DebugWriteAddress(const void* addr, DebugAction action)
  258. {
  259.     DebugWrite("\p@");
  260.     DebugWriteHex((long) addr, action);
  261. }
  262.  
  263. //------------------------------------------------------------------------------
  264.  
  265. void DebugWriteTypeAtAddress(const char* type, const void* addr, DebugAction action)
  266. {
  267.     DebugWrite(type);
  268.     DebugWriteAddress(addr, action);
  269. }
  270.  
  271. //------------------------------------------------------------------------------
  272.  
  273.